var experss = require('express');
var router = experss.Router();
var db = require("../public/javascripts/db");
var multer = require('multer');
var upload = multer();
var _ = require("lodash");

/* GET home page. */
router.post('/',upload.array(), function(req, res, next) {
    let account = _.trim(String(req.body.account));
    let pass = _.trim(String(req.body.pass));
    // let account = String(req.body.account);
    // let pass = String(req.body.pass);
    console.log("测试")
    let data = {
        isSuccess: false,
        data: null,
        msg: '登录失败！'
    };
    let sql = "select b.name depart, a.* from sys_user a left join sys_depart b on a.departid=b.id where account='" + account + "'";
    console.log(sql)
    db.execute(sql, function(err, rows) {
        if(err) {
            return res.json(data);
        } else {
            if((rows.length > 0) && (rows[0].password == pass) ) {
                var userid =  rows[0].id;
                var userName =  rows[0].name;
                var userType = rows[0].usertype;
                var role = rows[0].role;
                var depart = rows[0].depart;
                var departid = rows[0].departid;
                // console.log(rows[0]);
                data.data = { type: String(userType), role: role, depart: depart,departid: departid, id: userid, name: userName};
                data.isSuccess = true;
                data.msg = "登录成功";
                console.log("-----------------")
                console.log(data)
                return res.json(data);
            } else if(rows.length < 1){
                data.msg = "账号不存在！"
                return res.json(data);
            } else {
                data.msg = '密码不正确！';
                return res.json(data);
            }
        }
    })
});

module.exports = router;